package view
{
	import com.bit101.components.HBox;
	import com.bit101.components.HSlider;
	import com.bit101.components.ImageChangeBtn;
	import com.bit101.components.PushButton;
	import com.liu.load.LoadInfo;
	import com.liu.load.LoadManager;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class TabNav extends Sprite
	{
		private var btnSprite:HBox;
		private var source:Array;
		private var currentIndex:int;
		private var btnAry:Array;
		private var boxAry:Array;
		[Embed(source="assets/tab/tabbg.png")]
		private var bg:Class;
		
		[Embed(source="assets/tab/tabbtnUp.png")]
		private var up:Class;
		
		[Embed(source="assets/tab/tabbtnDown.png")]
		private var down:Class;
		
		public function TabNav(url:String)
		{
			this.addChild(new bg());
			var loaderinfo:LoadInfo = new LoadInfo(url,LoadInfo.XML,init); 
			LoadManager.getInstance().addSingleLoad(loaderinfo)
			btnSprite = new HBox(this,15,9);
			btnSprite.spacing = 1;
		}
		private function init(myxml:XML):void{
			source = new Array;
			for each(var classify:XML in myxml.tab){
				var ary:Array = new Array;
				for each(var prop:XML in classify.img){
					var obj:Object = new Object;
					obj.url = String(prop.@url);
					obj.url2 = String(prop.@url2);
					obj.orgW = int(prop.@orgW);
					obj.orgH = int(prop.@orgH);
					ary.push(obj);
				}
				var classObj:Object = new Object;
				classObj.data = ary;
				classObj.name = String(classify.@name);
				source.push(classObj);
			}
			
			btnAry = new Array;
			boxAry = new Array;
			for(var i:int;i<source.length;i++){
				var btn:ImageChangeBtn = new ImageChangeBtn(btnSprite,0,0,source[i].name,onClick);
				btn.width = 86;
				btn.setEmbedSource(up,down);
				btn.toggle = true;
				btnAry.push(btn);
				var cav:HCanvas = new HCanvas();
				cav.x = 20;
				cav.y = 42;
				this.addChild(cav);
				cav.visible = false;
				cav.source = source[i].data;
				boxAry.push(cav);
			}
			boxAry[0].visible = true;
			PushButton(btnAry[0]).selected = true;
			currentIndex = 0;
		}
		private function onClick(event:MouseEvent):void{
			var index:int = btnAry.indexOf(event.target);
			if(index == currentIndex){
				return;
			}
			boxAry[currentIndex].visible = false;
			PushButton(btnAry[currentIndex]).selected = false;
			
			boxAry[index].visible = true;
			PushButton(btnAry[index]).selected = true;
			
			currentIndex = index;
		}
	}
}